package org.dezenho.client.IU;

import java.awt.Color;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JLabel;
import javax.swing.JTextField;

import org.dezenho.client.SO.DezenhoClient;
import org.dezenho.common.RMI.ServerInterface;
/** Classe responsavel pela exibicao da interface inicial com o usuario, esta tambem 
 * captura o nome digitado pelo usuario e o conecta ao servidor.
 * @author dafneguisard
 */
public class Inicial extends GenericTela {
	
	private static final long serialVersionUID = 1L;
	private JDesktopPane jDesktopPane = null;
	private JTextField jTextField = null;
	private JButton jButton = null;
	private JLabel jLabel1 = null;
	private JLabel jLabel = null;
	private JLabel jLabel2 = null;
	private ServerInterface server;

	public Inicial() {
		super();
	}
	/**
	 * Metodo para criar a tela Inicial
	 * Implementa tambem a acao de desconectar o usuario do servidor quando a janela e fechada
	 */
	public void criaTela() {
		super.criaTela();
		this.setContentPane(getJDesktopPane());
		this.setTitle("Dezenho - Inicial");
		this.setVisible(true);
	}
	/**
	 * Metodo responsavel por coletar o nome do usuario digitado
	 * no campo de texto.
	 */
	public String coletaNome(){
		return getJTextField().getText();
	}
	private JDesktopPane getJDesktopPane() {
		if (jDesktopPane == null) {
			jLabel2 = new JLabel();
			getJLabel2().setBounds(new Rectangle(108, 15, 301, 97));
			getJLabel2().setIcon(getLogo());
			jLabel = new JLabel();
			getJLabel().setBounds(new Rectangle(172, 265, 194, 24));
			getJLabel().setFont(new Font("Arial", Font.BOLD, 18));
			getJLabel().setText("Entre com seu nome:");
			jLabel1 = new JLabel();
			getJLabel1().setBounds(new Rectangle(137, 138, 280, 50));
			getJLabel1().setDisplayedMnemonic(KeyEvent.VK_UNDEFINED);
			getJLabel1().setFont(new Font("Arial", Font.BOLD, 24));
			getJLabel1().setText("Acesso ao Servidor");
			jDesktopPane = new JDesktopPane();
			jDesktopPane.add(getJTextField(), null);
			jDesktopPane.setBackground(Color.WHITE);
			jDesktopPane.add(getJButton(), null);
			jDesktopPane.add(jLabel1, null);
			jDesktopPane.add(jLabel, null);
			jDesktopPane.add(jLabel2, null);
		}
		return jDesktopPane;
	}

	private JTextField getJTextField() {
		if (jTextField == null) {
			jTextField = new JTextField();
			jTextField.setFont(new Font("Arial", Font.PLAIN, 14));
			jTextField.setBounds(new Rectangle(92, 293, 352, 31));
		}
		return jTextField;
	}
	/**
	 * Metodo de acesso ao JButton associa a acao do botao ao metodo inicializar a
	 * sala de espera e conectar o usuario ao servidor.
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setBounds(new Rectangle(222, 333, 107, 32));
			jButton.setFont(new Font("Arial", Font.BOLD, 12));
			jButton.setText("Jogar");
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					getJTextField().setEditable(false);
					jButton.setEnabled(false);
					DezenhoClient.getInstance().getDezenhoTelaClient().inicializaTelaSalaEspera(new ArrayList<String>());
					DezenhoClient.getInstance().conectaServidor(coletaNome()); 
				}
			});
		}
		return jButton;
	}
	public JLabel getJLabel() {
		return jLabel;
	}
	public void setJLabel(JLabel label) {
		jLabel = label;
	}
	public JLabel getJLabel1() {
		return jLabel1;
	}
	public void setJLabel1(JLabel label1) {
		jLabel1 = label1;
	}
	public JLabel getJLabel2() {
		return jLabel2;
	}
	public void setJLabel2(JLabel label2) {
		jLabel2 = label2;
	}
	public void setJTextField(JTextField textField) {
		jTextField = textField;
	}
	public ServerInterface getServer() {
		return server;
	}

} 
